<?php
namespace Jpay\Helper;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;

! defined('DS') && define('DS', DIRECTORY_SEPARATOR);

/**
 * 捷贝-自定义方法、函数
 */

// 根据过滤器校验变量是否符合要求
function checkformat($variable, string $filter = "regexp",  string $regexp = ""): bool
{
    switch ($filter) {
        case "email":
            $ret = filter_var($variable, FILTER_VALIDATE_EMAIL);
            break;
        case "url":
            $ret = filter_var($variable, FILTER_VALIDATE_URL);
            break;
        case "ip":
            $ret = filter_var($variable, FILTER_VALIDATE_IP);
            break;
        case "int":
            $ret = filter_var($variable, FILTER_VALIDATE_INT);
            break;
        case "float":
            $ret = filter_var($variable, FILTER_VALIDATE_FLOAT);
            break;
        case "datetime":
            $ret = filter_var($variable, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^[0-9]{4}(\-|\/)[0-9]{1,2}(\\1)[0-9]{1,2}(|\s+[0-9]{1,2}(|:[0-9]{1,2}(|:[0-9]{1,2})))$/')));
            break;
        case "boolean":
            $ret = filter_var($variable, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^0|1$/')));
            break;
        case "mobile":
            $ret = filter_var($variable, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^1[3-9][0-9]{9,9}$/')));
            break;
        case "md5-16":
            $ret = filter_var($variable, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^[0-9a-f]{16,16}$/')));
            break;
        case "md5-32":
            $ret = filter_var($variable, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^[0-9a-fA-F]{32,32}$/')));
            break;
        case "money":
            $ret = filter_var($variable, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/')));
            break;
        case "numiid":
            $ret = filter_var($variable, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^\d{10,12}$/')));
            break;
        default :
            $ret = filter_var($variable, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $regexp)));
            break;
    }

    if ($ret !== false) return true;
    else return false;
}

// 字符串截取函数
function str_substr(string $str, string $start, string $end): string
{
    $tmp = explode($start, $str, 2);
    if (!$tmp[1]) return '';
    $content = explode($end, $tmp[1], 2);

    return isset($content[1]) ? $content[0] : '';
}

// 正则截取函数
function preg_substr(string $str, string $start, string $end): string
{
    $tmp = preg_split($start, $str);
    $content = preg_split($end, $tmp[1]);
    return $content[0];
}

// 输入参数依次为原始字符串,路径名称或起始截取字符串,格式(xml,json,other),截止字符串
function cut_str(string $raw, string $path, string $format = "xml", string $endstr = "")
{
    if ($format == "xml") {
        $obj = simplexml_load_string($raw);
        return eval('return $obj->' . $path . ';');
    }
    // gbk转utf-8才能正确处理字符串切割
    if (mb_detect_encoding($raw, "ASCII,GB2312,GBK,UTF-8") == "EUC-CN") $raw = iconv("GB2312", "UTF-8//IGNORE", $raw);
    if ($format == "json") {
        $obj = json_decode($raw);
        return eval('return $obj->' . $path . ';');
    } else {
        if ($endstr == "") return false;
        $a = strpos($raw, $path, 0);
        $b = strpos($raw, $endstr, $a);
        if (!$b) return false;
        return substr($raw, $a + strlen($path), $b - ($a + strlen($path)));
    }
}

function wk_monolog(array $data, string $path = 'test', string $level = 'info', string $time = 'd')
{
    if (empty($data) || (empty($data['msg']) && empty($data['data']))) return false;
    $dateStr = ['d' => 'Y-m-d', 'h' => 'Y-m-d.H'];
    $logFile = date($dateStr[strtolower($time)] ?: $time).'.log';
    $logPath = dirname(__DIR__, 2).DS.'runtime'.DS.'logs'.DS.trim(str_replace(['/', '\/'], DS, $path), '\\/');
    $streamHanler = new StreamHandler($logPath.DS.$logFile, Logger::DEBUG);

    // Create the logger
    $logger = new Logger('jpay');
    // Now add some handlers
    $logger->pushHandler($streamHanler);
    // $logger->pushHandler(new FirePHPHandler());

    // You can now use your logger
    // $logger->info('My logger is now ready');
    if (empty($data['msg'])) $data['msg'] = 'LOG TEST';
    $logger->{$level}($data['msg'], $data['data']);
}

// 根据 shoptype 获取相关数据表名
function get_shop_table(int $shopType): array
{
    $nameSpace = __NAMESPACE__;
    if ($shopType < 20) {
        $orderTable = 'o_realtime';
        $orderEntity = 'ORealtime';
        $priceTable = 'o_price';
        $priceEntity = 'OPrice';
        $rebateTable = 'o_rebate';
        $rebateEntity = 'ORebate';
        $channelTable = 'b_channel';
        $channelEntity = 'bChannel';
    } elseif ($shopType < 30) {
        $orderTable = 'o_flowtime';
        $orderEntity = 'OFlowtime';
        $priceTable = 'o_price_flow';
        $priceEntity = 'OPriceFlow';
        $rebateTable = 'o_rebate_flow';
        $rebateEntity = 'ORebateFlow';
        $channelTable = 'b_channel_flow';
        $channelEntity = 'bChannelFlow';
    } elseif ($shopType < 40) {
        $orderTable = 'o_lifetime';
        $orderEntity = 'oLifetime';
        $priceTable = 'o_price';
        $priceEntity = 'OPrice';
        $rebateTable = 'o_rebate';
        $rebateEntity = 'ORebate';
        $channelTable = 'b_channel';
        $channelEntity = 'bChannel';
    } elseif ($shopType > 200) {
        $orderTable = 'o_flowtime';
        $orderEntity = 'OFlowtime';
        $priceTable = 'o_price_flow';
        $priceEntity = 'OPriceFlow';
        $rebateTable = 'o_rebate_flow';
        $rebateEntity = 'ORebateFlow';
        $channelTable = 'b_channel_flow';
        $channelEntity = 'bChannelFlow';
    } else {
        return [];
    }
    return compact('orderTable', 'orderEntity', 'priceTable', 'priceEntity', 'rebateTable', 'rebateEntity', 'channelTable', 'channelEntity', 'nameSpace');
}
/* END */